<?php
namespace app\api\controller\xiservice;

use app\common\controller\xiservice\XiserviceApi;
use app\common\model\xiservice\Benfit;
use app\common\model\xiservice\User;
use think\Exception;
use think\Db;
/**
 * 提现模块
 */
class Withdraw extends XiserviceApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model= new \app\common\model\xiservice\Withdraw();

    }

    /**
     * @ApiTitle (服务人员提现记录)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xiservice.withdraw/list)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'int','order':'object','money':'float','createtime':'int','createtime_text':'date'}", description="返回数据")
     */
    public function list() {
       
        $list = $this->model->where('user_id',$this->auth->id)
                ->field('id,money,checkinfo,createtime')
                ->order('createtime desc')
                ->paginate();
        $this->success('查询成功',$list);
    }


    /**
     * @ApiTitle (服务人员申请提现)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xiservice.withdraw/apply)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiParams   (name="name", type="int", description="开户姓名")
     * @ApiParams   (name="bankname", type="int", description="开户行")
     * @ApiParams   (name="bankcard", type="int", description="开户账户")
     * @ApiParams   (name="money", type="int", description="提现金额")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="返回数据")
     */
    public function apply() {
        $param=$this->request->post('','','trim,strip_tags');
        $param['user_id']=$this->auth->id;
        $user =  User::where('user_id',$param['user_id'])->find();
        if(!$user){
            $this->error('token错误');
        }
        if($param['money']<=0){
            $this->error('提现金额输入错误!');
        }
        if(abs($param['money'])>$user['can_money']){
                 $this->error('提现金额超出可提现额度!');
        }
        Db::startTrans();
        try {  

            $res=$this->model->allowField(true)->save($param);
            //冻结金额增加 
            $freezeMoney = User::freeze($param['money'],$param['user_id'], $this->model->id,2);
            //可提现金额减少
            $canMoney = User::money(-abs($param['money']), $param['user_id'],'提现申请');
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }        
        $this->success('申请成功!等待后台审核',[]);
    }


}